package xyz.syyrjx.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.syyrjx.annotions.RequireLogin;
import xyz.syyrjx.annotions.RestrictCalls;
import xyz.syyrjx.annotions.Symbol;
import xyz.syyrjx.entity.Order;
import xyz.syyrjx.entity.ResultInfo;
import xyz.syyrjx.entity.User;
import xyz.syyrjx.service.MysqlRemoteService;
import xyz.syyrjx.service.RedisRemoteService;
import xyz.syyrjx.utils.JWTUtil;
import xyz.syyrjx.utils.TokenUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 订单controller
 *
 * @author Mr.Wan
 * @date 2022/10/11 - 19:36
 */
@RestController
public class OrderController {

    @Resource
    private MysqlRemoteService mysqlService;
    @Resource
    private RedisRemoteService redisService;

    @RestrictCalls(count = 3)
    @RequireLogin(authorities = {"order-get","order-delete"},
                    roles = {"normal"},authoritiesRolesConnection = Symbol.AND)
    @GetMapping("/getMyOrder")
    public ResultInfo<List<Order>> getOrderSelf(HttpServletRequest request){
        String token = JWTUtil.getToken(request);
        ResultInfo<User> tokenUser = redisService.getUser(token);
        User user = tokenUser.getData();
        ResultInfo<List<Order>> orderSelf = mysqlService.getOrderSelf(user);
        if (orderSelf.isFlag()){
            return ResultInfo.success(orderSelf.getData());
        }else{
            return orderSelf;
        }

    }
}
